package com.app;

import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

import com.admin.model.Orders;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;

public class FrontOrdersController extends Controller{
	// 添加订单
	public void addOrder(){
		getResponse().addHeader("Access-Control-Allow-Origin", "*");
		getResponse().addHeader("Access-Control-Allow-Methods","GET, POST, OPTIONS");
		getResponse().addHeader("Access-Control-Allow-Headers",
				"Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With");
		JSONObject obj = new JSONObject();
		try {
			Orders orders = getModel(Orders.class);
			String source = orders.get("source");
			if(null != source && source != ""){
				source = URLDecoder.decode(source,"utf-8");
				orders.set("source",source);
			}
			orders.set("create_date",new Date());
			/*String qudao = Orders.dao.getQudao(getRequest());
			orders.set("source",qudao);*/
			String time = String.valueOf(new Date().getTime());
			String agordernum = time + (int)(Math.random()*100);
			orders.set("order_no",agordernum);
			orders.save();
			obj.put("state",1);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("state",0);
		}
		renderJson(obj);
	}
	
	public void test(){
		/*String qudao = "";
		String url = UtilTools.getUrl(getRequest());
		int startIndex = url.indexOf("q=");
		if(url.indexOf("&") != -1){
			qudao = url.substring(startIndex+2,url.indexOf("&"));
		}else{
			qudao = url.substring(startIndex+2);
		}
		System.out.println(qudao);*/
		
		List<Orders> list = Orders.dao.query(null);
		int i = 0;
		for (Orders orders : list) {
			String time = String.valueOf(new Date().getTime());
			String agordernum = time + (int)(Math.random()*100);
			orders.set("order_no",agordernum);
			orders.update();
			i++;
			System.out.println(i);
		}
	}
}
